//
//  HBOriginalView.swift
//  sinaWeibo
//
//  Created by Beans on 16/6/26.
//  Copyright © 2016年 iceWorks. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

// 简便: 定义为CGFloat, 有需要时再更改
let HomeCellMargin: CGFloat = 10

// 原创微博视图
class HBOriginalView: UIView {

    // (1)
    var bottomConstraint: Constraint?

    var originalViewModel: HBStatusCellViewModel? {
        didSet {
            /*
            // 头像
            if let urlString = originalViewModel?.model?.user?.profile_image_url {
                let url = NSURL(string: urlString)
                phontImageView.sd_setImageWithURL(url)
            }
            // 昵称
            nameLabel.text = originalViewModel?.model?.user?.name
            // 内容
            contentLabel.text = originalViewModel?.model?.text
            // vip
            if let level = originalViewModel?.model?.user?.mbrank {
                vipImageView.image = UIImage(named: "common_icon_membership_level\(level)")
            }
            // 认证标志
            /// 认证类型 -1：没有认证，0，认证用户，2,3,5: 企业认证，220: 达人
            if let type = originalViewModel?.model?.user?.verified_type {
                switch type {

                case -1: verifitiedImageView.image = UIImage()
                case 0: verifitiedImageView.image = UIImage(named: "avatar_grassroot")
                case 2,3,5: verifitiedImageView.image = UIImage(named: "avatar_enterprise_vip")
                case 220: verifitiedImageView.image = UIImage(named: "avatar_vip")

                default : verifitiedImageView.image = UIImage()
                }
            }
            */

            phontImageView.sd_setImageWithURL(originalViewModel?.photoURL)
            nameLabel.text = originalViewModel?.name
//            contentLabel.text = originalViewModel?.content
            contentLabel.attributedText = originalViewModel?.originalAttributeString
            vipImageView.image = originalViewModel?.vipImage
            verifitiedImageView.image = originalViewModel?.verifyImage
            // 设置来源字段
            sourceLabel.text = originalViewModel?.sourceString
            // 时间字段
            timeLabel.text = originalViewModel?.createTimeString

            // (2) 卸载约束
            bottomConstraint?.uninstall()

            // cell重用导致约束重复
            if originalViewModel?.model?.pic_urls?.count > 0 {
                printLog("显示原创配图")
                pictureView.hidden = false
                // 更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    // (3) 为cell重用准备
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                // 传递pictureView数据
                pictureView.picURLs = originalViewModel?.model?.pic_urls

            } else {
                printLog("隐藏原创配图")
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            }
        }
    }

    // 1> 构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // 2> 添加与布局子控件
    private func setupUI() {
        addSubview(phontImageView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(vipImageView)
        addSubview(contentLabel)
        addSubview(verifitiedImageView)
        // 图片
        addSubview(pictureView)

        phontImageView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(10)
            make.top.equalTo(self.snp_top).offset(10)
            make.width.equalTo(40)
            make.height.equalTo(40)
        }

        verifitiedImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(phontImageView.snp_right)
            make.centerY.equalTo(phontImageView.snp_bottom)
        }

        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(phontImageView.snp_right).offset(HomeCellMargin)
            make.top.equalTo(phontImageView.snp_top)
        }

        vipImageView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel.snp_centerY)
            make.left.equalTo(nameLabel.snp_right).offset(HomeCellMargin)
        }

        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_left)
            make.top.equalTo(nameLabel.snp_bottom).offset(0.5 * HomeCellMargin)
        }

        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(timeLabel.snp_centerY)
            make.left.equalTo(timeLabel.snp_right).offset(HomeCellMargin)
        }

        contentLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(phontImageView.snp_left)
            make.top.equalTo(phontImageView.snp_bottom).offset(HomeCellMargin)
            make.right.equalTo(self.snp_right).offset(HomeCellMargin).offset(-HomeCellMargin)
            make.bottom.equalTo(pictureView.snp_top).offset(-HomeCellMargin * 0.5)
        }

        // 图片
        pictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(self.snp_left).offset(HomeCellMargin)
            // temp
//            make.width.height.equalTo(100)
        }

        // 自动计算行高 (尤其底部)
        self.snp_makeConstraints { (make) -> Void in
            // (3)
            self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
    }

    // 3> lazy load
    // 头像
    private lazy var phontImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "avatar_default_big")
        img.layer.cornerRadius = 40 / 2.0
        img.layer.masksToBounds = true
        return img
    }()

    // 昵称
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.text = "昵称"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(17.0)
        return label
    }()

    // 时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.text = "时间"
        label.textColor = UIColor.orangeColor()
        label.font = UIFont.systemFontOfSize(13.0)
        return label
    }()

    // 来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()
        label.text = "来源"
        label.textColor = UIColor.orangeColor()
        label.font = UIFont.systemFontOfSize(13.0)
        return label
    }()

    // vip图标
    lazy var vipImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "common_icon_membership")
        return img
    }()

    // 认证图片
    lazy var verifitiedImageView: UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "avatar_grassroot")
        return img
    }()

    // 内容
    lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.text = "这里是微博内容"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(16.0)
        label.numberOfLines = 0
        return label
    }()

    /// 微博图片
    private lazy var pictureView: HBPictureView = HBPictureView()
}
